package io.renren.modules.demo.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.demo.dto.DhClubearningsInfoDTO;
import io.renren.modules.demo.dto.DhSportapplyInfoDTO;
import io.renren.modules.demo.excel.DhClubearningsInfoExcel;
import io.renren.modules.demo.service.DhClubearningsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


/**
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-03-25
 */
@RestController
@RequestMapping("demo/dhclubearningsinfo")
@Api(tags = "")
public class DhClubearningsInfoController {
    @Autowired
    private DhClubearningsInfoService dhClubearningsInfoService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({@ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"), @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String")})
    @RequiresPermissions("demo:dhclubearningsinfo:page")
    public Result<PageData<DhClubearningsInfoDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<DhClubearningsInfoDTO> page = dhClubearningsInfoService.page(params);

        return new Result<PageData<DhClubearningsInfoDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("demo:dhclubearningsinfo:info")
    public Result<DhClubearningsInfoDTO> get(@PathVariable("id") Long id) {
        DhClubearningsInfoDTO data = dhClubearningsInfoService.get(id);

        return new Result<DhClubearningsInfoDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("demo:dhclubearningsinfo:save")
    public Result save(@RequestBody DhClubearningsInfoDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        dhClubearningsInfoService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("demo:dhclubearningsinfo:update")
    public Result update(@RequestBody DhClubearningsInfoDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        dhClubearningsInfoService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("demo:dhclubearningsinfo:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        dhClubearningsInfoService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("demo:dhclubearningsinfo:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<DhClubearningsInfoDTO> list = dhClubearningsInfoService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, DhClubearningsInfoExcel.class);
    }

    @GetMapping("/getDataListByOrderId/{orderId}")
    @ApiOperation("根据订单编号查询订单信息")
    @LogOperation("根据订单编号查询订单信息")
    public Result<DhClubearningsInfoDTO> getDataListByOrderId(@PathVariable("orderId") Integer orderId) {
        DhClubearningsInfoDTO data = dhClubearningsInfoService.getDataListByOrderId(orderId);
        return new Result<DhClubearningsInfoDTO>().ok(data);
    }

    @GetMapping("/getChildFiveData")
    @ApiOperation("获取俱乐部总收益")
    @LogOperation("获取俱乐部总收益")
    public Result<Integer> getChildFiveData() {
        Integer data = dhClubearningsInfoService.getChildFiveData();
        return new Result<Integer>().ok(data);
    }

    @GetMapping("/getChildCenterData")
    @ApiOperation("获取近五年俱乐部收益数据")
    @LogOperation("获取近五年俱乐部收益数据")
    public Result<BigDecimal> getChildCenterData(@Param("year") String year) {
        BigDecimal data = dhClubearningsInfoService.getChildCenterData(year);
        return new Result<BigDecimal>().ok(data);
    }
}